// $codepro.audit.disable largeNumberOfParameters, stringConcatenationInLoop
/**
 * Nurse class
 */
package edu.gatech.userclasses;


import java.util.ArrayList;
import java.util.Collection;

import edu.gatech.datastorage.Appointment;
import edu.gatech.datastorage.Gender;
import edu.gatech.datastorage.PatientInformation;
import edu.gatech.datastorage.TreatmentRecord;
import edu.gatech.utilities.Persistence;

/**
 * Nurse class can manage patient database, view appointments, and
 * create treatment records.
 * @author Dan Holdridge
 * @version 1.0
 */
public class Nurse extends User {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6359186731917151040L;

	/**
	 * Creates a Nurse given a name
	 * @param name the Nurse's name
	 */
	public Nurse(String name) {
		this.name = name;
		this.userType = "Nurse";
	}
	
	/**
	 * Constructor used for creating account
	 * @param username
	 * @param password
	 */
	public Nurse(String username, String password){
		this.username = username;
		this.password = password;
		this.name = username;
		this.userType = "Nurse";
	}
	
	/**
	 * Creates a Nurse with a default name
	 */
	public Nurse() {
		this("John Doe");
	}
		
	/**
	 * Creates a new patient.
	 * @param name Patient's name
	 * @param password Patient's password to log in to system
	 * @param age The age of the patient
	 * @param gender The patien's gender
	 * @param phoneNumber The patient's phone number
	 * @param address The patient's address
	 * @param pharmacy The patient's pharmacy of choice
	 * @param insurance The patient's insurance provider
	 * @param medication A list of medications the patient is taking
	 * @param allergies A list of the patient's allergies
	 * @param dateOfBirth The patient's date of birth
	 */
	public void addPatient(String name, String password, int age, Gender 
			gender, String phoneNumber, String address, String pharmacy, 
			String insurance, Collection<String> medication, Collection<String>
			allergies, String dateOfBirth) {
		final Patient patient = new Patient(new PatientInformation(name, password, age,
				gender, phoneNumber, address, pharmacy, insurance, medication, 
				allergies, dateOfBirth) );
		patient.setusername(name);
		
		Persistence.getUserList().add(patient);
		Persistence.refresh();
	}
	
	/**
	 * Adds patient
	 * @param info
	 */
	public void addPatient(PatientInformation info){
		final Patient patient = new Patient(info);
		
		Persistence.getUserList().add(patient);
		Persistence.refresh();
		Persistence.save();
	}
	
	/**
	 * Deletes a patient from the office records
	 * @param p The patient to be deleted
	 */
	public void deletePatient(Patient p) {
		
		Persistence.getUserList().remove(p);
		Persistence.refresh();
	}
	
	/**
	 * View a list of appointments
	 * @param p The Patient whose appointments are being
	 * viewed
	 * @return A Collection of strings representing the dates of each
	 * appointment in p's patient history
	 *///TODO Temporarily viewing all appointments for a selected patient
	public Collection<String> viewAppointments(Patient p) {
		final Collection<TreatmentRecord> list = p.getInfo().getHistory();
		final Collection<String> appointments = new ArrayList<String>(list.size() );
		for (TreatmentRecord record : list) {
			appointments.add( record.toString() );
		}
		return appointments;
	}
	
	/**
	 * Creates a new treatment record and adds it to a patient's history.
	 * @param pat The Patient getting a new treatment record
	 * @param nurse The String name of the nurse
	 * @param doctor The String name of the doctor
	 * @param symptoms The String of symptoms the patient is showing
	 * @param vitalSigns The String containing info like blood pressure, temperature,
	 * etc
	 * @param diagnosis The String containing the doctor's diagnosis of the patient.
	 */
	public void createTreatmentRecord(Patient pat, String nurse, String doctor, 
			String symptoms, String vitalSigns, String diagnosis) {
		/*TODO 
		 * See about searching for a specific TreatmentRecord instead of 
		 * just using the last one in the list
		 */
		final TreatmentRecord record = pat.getInfo().getLastRecord();
		record.setDiagnosis(diagnosis);
		record.setDoctor(doctor);
		record.setNurse(nurse);
		record.setSymptoms(symptoms);
		record.setVitalSigns(vitalSigns);
		
	}
	
	/** 
	 * Searches for patient given the patient's username
	 * author: Will Folsom 
	 * @param name The patient's username
	 * @return Patient
	 */
	public Patient searchForPatient(String name){
		for (User p : Persistence.getUserList()){
			if(p.getUserType().equals("Patient")){
				if (((Patient) p).getInfo().getName().equals(name)){
					return (Patient) p;
				}
			}
		}
		return null;
	}
	
	/**Searches for all appointments
	 * 
	 * @return list of appointments
	 */
	public String searchForAppointments(){
		String appointments = "";
		for (User p : Persistence.getUserList()){
			if(p.getUserType().equals("Patient")){
				Patient patient = (Patient) p;
				if(patient.getInfo().getLastRecord() != null){
					Appointment appointment = 
						patient.getInfo().getLastRecord().getAppointment();
					appointments = appointments + 
					appointment.toString() + "\n";
				}
			}
		}
		return appointments;
	}
	
	/**
	 * Edits a nurse with additional or altered information
	 * @param name
	 * @param username
	 * @param password
	 * @param address
	 * @param phoneNumber
	 */
	public void editNurse(String name, String username, String password, 
			String address, String phoneNumber){
		this.setName (name);
		this.setusername(username);
		this.setPassword(password);
		this.setAddress(address);
		this.setPhoneNumber(phoneNumber);
	}
	
	/**
	 * Deletes this instance of a nurse
	 */
	public void deleteNurse(){
		Persistence.getUserList().remove(this);
		Persistence.refresh();
	}
}
